package com.ithiem.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.dubbo.api.OptionsApi;
import com.itheima.model.domain.Options;
import com.itheima.model.dto.QuestionsDto;
import com.ithiem.dubbo.mappers.OptionsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class OptionsApiImpl implements OptionsApi {
    @Autowired
    private OptionsMapper optionsMapper;

    @Override
    public void saveAll(List<Options> optionsList) {
        optionsList.forEach(o->optionsMapper.insert(o));
    }

    @Override
    public Integer concludeScore(List<QuestionsDto> answers) {
        Integer score = 0;
        for (QuestionsDto answer : answers) {
            LambdaQueryWrapper<Options> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Options::getQuestionsId,answer.getQuestionId()).eq(Options::getId,answer.getOptionId());//根据试题编号和选项编号去计算分数
            score +=optionsMapper.selectOne(lqw).getScore();
        }
        return score;
    }

    @Override
    public List<Options> listByQuestionsId(Long questionId) {
        LambdaQueryWrapper<Options> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Options::getQuestionsId,questionId).orderByAsc(Options::getNumber);
        return optionsMapper.selectList(lqw);
    }
}
